package javaMSCRM;
import javax.swing.JOptionPane;


import com.microsoft.schemas.crm._2007.webservices.DeleteDocument;
import com.microsoft.schemas.crm._2007.webservices.EntityName;
import com.microsoft.schemas.crm._2007.webservices.DeleteDocument.Delete;

/**
 * @author Zoran IVANOV
 *
 */
public class DeleteEntities {
	JavaMSCRM JavaMSCRM = null;

	/**
	 * @param JavaMSCRM
	 */
	public DeleteEntities(JavaMSCRM JavaMSCRM) {
		this.JavaMSCRM = JavaMSCRM;
		String msg ="Entries succesfuly deleted";
		int[] SelectedRows = JavaMSCRM.table.getSelectedRows();
		if(SelectedRows.length > 0){
			int deleteConfirmation = JOptionPane.showConfirmDialog(null,
				       		"Are you sure you want to delete the selected records?",
				       		"Records Deletion Confirmation", 
					        JOptionPane.YES_NO_OPTION);
			
			if (deleteConfirmation == JOptionPane.YES_OPTION){
				for(int i=0; i < SelectedRows.length; i++){
					try {
						DeleteDocument dd = DeleteDocument.Factory.newInstance();
						Delete d = Delete.Factory.newInstance();
						d.setId(JavaMSCRM.EntriesIDList.get(SelectedRows[i]));
						d.setEntityName(JavaMSCRM.Entity);
						
						dd.setDelete(d);
						
						JavaMSCRM.stub.Delete(dd, JavaMSCRM.catd, null, null);
					}  
					catch (Exception e) {
						msg ="There was a problem while deleting some of the entries!";
					}
				}
				
				if(JavaMSCRM.Entity == EntityName.ACTIVITYPOINTER.toString()){
					new Activities(JavaMSCRM);
				}
				if(JavaMSCRM.Entity == EntityName.ACCOUNT.toString()){
					new Accounts(JavaMSCRM);
				}
				if(JavaMSCRM.Entity == EntityName.CONTACT.toString()){
					new Contacts(JavaMSCRM);
				}
				if(JavaMSCRM.Entity == EntityName.LEAD.toString()){
					new Leads(JavaMSCRM);
				}
				if(JavaMSCRM.Entity == EntityName.OPPORTUNITY.toString()){
					new Opportunities(JavaMSCRM);
				}
				JavaMSCRM.MainMessage.setText(msg);
			}
		}
	}
}
